home *** CD-ROM | disk | FTP | other *** search
/ Champak 83 / (Vol 83) My Disc.iso / Games / hello_kitty.swf / scripts / frame_10 / DoAction.as
Encoding:
Text File  |  2008-08-07  |  13.3 KB  |  470 lines

  1. function buildMap(map)
  2. {
  3.    function enemyBrain()
  4.    {
  5.       var i = 0;
  6.       while(i < 5)
  7.       {
  8.          var name = "enemy" + i;
  9.          var ob = game[name];
  10.          getMyCorners(ob.x + ob.speed * ob.xMove,ob.y + ob.speed * ob.yMove,ob);
  11.          if(ob.downleft and ob.upleft and ob.downright and ob.upleft)
  12.          {
  13.             moveChar(ob,ob.xMove,ob.yMove);
  14.          }
  15.          else
  16.          {
  17.             ob.xMove = - ob.xMove;
  18.             ob.yMove = - ob.yMove;
  19.          }
  20.          if(!_root.infiniteLife)
  21.          {
  22.             var xdist = ob.x - char.x;
  23.             var ydist = ob.y - char.y;
  24.             if(math.sqrt(xdist * xdist + ydist * ydist) < ob.width + char.width)
  25.             {
  26.                _root.lifes -= 1;
  27.                mylifes.gotoAndStop(_root.lifes);
  28.                if(_root.lifes == 0)
  29.                {
  30.                   removeMovieClip(_root.tiles);
  31.                   _root.gotoAndStop(35);
  32.                }
  33.                if(!_root.hurt)
  34.                {
  35.                   char.clip.gotoAndPlay("hurt");
  36.                   return undefined;
  37.                }
  38.             }
  39.          }
  40.          i++;
  41.       }
  42.    }
  43.    function detectKeys()
  44.    {
  45.       var ob = _root.char;
  46.       var keypressed = false;
  47.       if(key.isDown(39))
  48.       {
  49.          keyPressed = _root.moveChar(ob,1,0);
  50.       }
  51.       else if(key.isDown(37))
  52.       {
  53.          keyPressed = _root.moveChar(ob,-1,0);
  54.       }
  55.       else if(key.isDown(38))
  56.       {
  57.          keyPressed = _root.moveChar(ob,0,-1);
  58.       }
  59.       else if(key.isDown(40))
  60.       {
  61.          keyPressed = _root.moveChar(ob,0,1);
  62.       }
  63.       if(!KeyPressed)
  64.       {
  65.          ob.clip.mychar.gotoAndStop(1);
  66.       }
  67.       else
  68.       {
  69.          ob.clip.mychar.play();
  70.       }
  71.       enemyBrain();
  72.    }
  73.    function moveChar(ob, dirx, diry)
  74.    {
  75.       getMyCorners(ob.x,ob.y + ob.speed * diry,ob);
  76.       if(diry == -1)
  77.       {
  78.          if(ob.upleft and ob.upright)
  79.          {
  80.             ob.y += ob.speed * diry;
  81.          }
  82.          else
  83.          {
  84.             ob.y = ob.ytile * game.tileH + ob.height;
  85.          }
  86.       }
  87.       if(diry == 1)
  88.       {
  89.          if(ob.downleft and ob.downright)
  90.          {
  91.             ob.y += ob.speed * diry;
  92.          }
  93.          else
  94.          {
  95.             ob.y = (ob.ytile + 1) * game.tileH - ob.height;
  96.          }
  97.       }
  98.       getMyCorners(ob.x + ob.speed * dirx,ob.y,ob);
  99.       if(dirx == -1)
  100.       {
  101.          if(ob.downleft and ob.upleft)
  102.          {
  103.             ob.x += ob.speed * dirx;
  104.          }
  105.          else
  106.          {
  107.             ob.x = ob.xtile * game.tileW + ob.width;
  108.          }
  109.       }
  110.       if(dirx == 1)
  111.       {
  112.          if(ob.upright and ob.downright)
  113.          {
  114.             ob.x += ob.speed * dirx;
  115.          }
  116.          else
  117.          {
  118.             ob.x = (ob.xtile + 1) * game.tileW - ob.width;
  119.          }
  120.       }
  121.       ob.clip._x = ob.x;
  122.       ob.clip._y = ob.y;
  123.       ob.clip.gotoAndStop(dirx + diry * 2 + 3);
  124.       ob.xtile = Math.floor(ob.clip._x / game.tileW);
  125.       ob.ytile = Math.floor(ob.clip._y / game.tileH);
  126.       var itemname = game["item" + ob.ytile + "_" + ob.xtile];
  127.       if(itemname and ob == _root.char)
  128.       {
  129.          game.points = true;
  130.          removeMovieClip(itemname.clip);
  131.          game.items[itemname.position] = 0;
  132.          delete game["item" + ob.ytile + "_" + ob.xtile];
  133.          var name = "enemy3";
  134.          game[name] = new game.Enemyp3();
  135.          game.clip.attachMovie("enemy1",name,10005);
  136.          game[name].clip = game.clip[name];
  137.          game[name].xtile = 4;
  138.          game[name].ytile = 2;
  139.          game[name].width = game.clip[name]._width / 2;
  140.          game[name].height = game.clip[name]._height / 2;
  141.          game[name].x = game[name].xtile * game.tileW + game.tileW / 2;
  142.          game[name].y = game[name].ytile * game.tileH + game.tileH / 2;
  143.          game[name].clip._x = game[name].x;
  144.          game[name].clip._y = game[name].y;
  145.          if(game.points = true)
  146.          {
  147.             opendoorname = "t_" + unlockY + "_" + unlockX;
  148.             game.clip[opendoorname].gotoAndStop(4);
  149.             game.Tile2.prototype.walkable = true;
  150.          }
  151.       }
  152.       if(_root.char.ytile == unlockY && _root.char.xtile == unlockX)
  153.       {
  154.          removeMovieClip(_root.tiles);
  155.          _root.gotoAndStop(11);
  156.       }
  157.       if(_root.char.ytile == 4 && _root.char.xtile == 4)
  158.       {
  159.          var name = "enemy3";
  160.          game[name].xMove = 0;
  161.          game[name].yMove = -1;
  162.          game[name].speed = 0.5;
  163.       }
  164.       return true;
  165.    }
  166.    function getMyCorners(x, y, ob)
  167.    {
  168.       ob.downY = Math.floor((y + ob.height - 1) / game.tileH);
  169.       ob.upY = Math.floor((y - ob.height) / game.tileH);
  170.       ob.leftX = Math.floor((x - ob.width) / game.tileW);
  171.       ob.rightX = Math.floor((x + ob.width - 1) / game.tileW);
  172.       ob.upleft = game["t_" + ob.upY + "_" + ob.leftX].walkable;
  173.       ob.downleft = game["t_" + ob.downY + "_" + ob.leftX].walkable;
  174.       ob.upright = game["t_" + ob.upY + "_" + ob.rightX].walkable;
  175.       ob.downright = game["t_" + ob.downY + "_" + ob.rightX].walkable;
  176.    }
  177.    _root.attachMovie("empty","tiles",++d);
  178.    game.clip = _root.tiles;
  179.    game.clip._x = gameInitial_x;
  180.    game.clip._y = gameInitial_y;
  181.    var mapWidth = map[0].length;
  182.    var mapHeight = map.length;
  183.    var i = 0;
  184.    while(i < mapHeight)
  185.    {
  186.       var j = 0;
  187.       while(j < mapWidth)
  188.       {
  189.          var name = "t_" + i + "_" + j;
  190.          game[name] = new game["Tile" + map[i][j]]();
  191.          game.clip.attachMovie("tile",name,i * 100 + j * 2);
  192.          game.clip[name]._x = j * game.tileW;
  193.          game.clip[name]._y = i * game.tileH;
  194.          game.clip[name].gotoAndStop(game[name].frame);
  195.          j++;
  196.       }
  197.       i++;
  198.    }
  199.    game.items = myItems;
  200.    var i = 0;
  201.    while(i < game.items.length)
  202.    {
  203.       var name = "item" + game.items[i][2] + "_" + game.items[i][1];
  204.       game[name] = new game["Item" + game.items[i][0]]();
  205.       game[name].position = i;
  206.       game.clip.attachMovie("item",name,10001 + i);
  207.       game[name].clip = game.clip[name];
  208.       game[name].clip._x = game.items[i][1] * game.tileW + game.tileW / 2;
  209.       game[name].clip._y = game.items[i][2] * game.tileH + game.tileH / 2;
  210.       game[name].clip.gotoAndStop(game.items[i][0]);
  211.       i++;
  212.    }
  213.    var enemies = myEnemies;
  214.    game.currentEnemies = enemies.length;
  215.    var i = 0;
  216.    while(i < game.currentEnemies)
  217.    {
  218.       var name = "enemy" + i;
  219.       game[name] = new game["Enemyp" + enemies[i][0]]();
  220.       game.clip.attachMovie("enemy1",name,10021 + i);
  221.       game[name].clip = game.clip[name];
  222.       game[name].xtile = enemies[i][1];
  223.       game[name].ytile = enemies[i][2];
  224.       game[name].width = game.clip[name]._width / 2;
  225.       game[name].height = game.clip[name]._height / 2;
  226.       game[name].x = game[name].xtile * game.tileW + game.tileW / 2;
  227.       game[name].y = game[name].ytile * game.tileH + game.tileH / 2;
  228.       game[name].clip._x = game[name].x;
  229.       game[name].clip._y = game[name].y;
  230.       i++;
  231.    }
  232.    game.clip.attachMovie("char","char",1000);
  233.    char.clip = game.clip.char;
  234.    char.x = char.xtile * game.tileW + game.tileW / 2;
  235.    char.y = char.ytile * game.tileH + game.tileH / 2;
  236.    char.width = char.clip._width / 2;
  237.    char.height = char.clip._height / 2;
  238.    char.clip._x = char.x;
  239.    char.clip._y = char.y;
  240. }
  241. function enemyBrain()
  242. {
  243.    var i = 0;
  244.    while(i < 5)
  245.    {
  246.       var name = "enemy" + i;
  247.       var ob = game[name];
  248.       getMyCorners(ob.x + ob.speed * ob.xMove,ob.y + ob.speed * ob.yMove,ob);
  249.       if(ob.downleft and ob.upleft and ob.downright and ob.upleft)
  250.       {
  251.          moveChar(ob,ob.xMove,ob.yMove);
  252.       }
  253.       else
  254.       {
  255.          ob.xMove = - ob.xMove;
  256.          ob.yMove = - ob.yMove;
  257.       }
  258.       if(!_root.infiniteLife)
  259.       {
  260.          var xdist = ob.x - char.x;
  261.          var ydist = ob.y - char.y;
  262.          if(math.sqrt(xdist * xdist + ydist * ydist) < ob.width + char.width)
  263.          {
  264.             _root.lifes -= 1;
  265.             mylifes.gotoAndStop(_root.lifes);
  266.             if(_root.lifes == 0)
  267.             {
  268.                removeMovieClip(_root.tiles);
  269.                _root.gotoAndStop(35);
  270.             }
  271.             if(!_root.hurt)
  272.             {
  273.                char.clip.gotoAndPlay("hurt");
  274.                return undefined;
  275.             }
  276.          }
  277.       }
  278.       i++;
  279.    }
  280. }
  281. function detectKeys()
  282. {
  283.    var ob = _root.char;
  284.    var keypressed = false;
  285.    if(key.isDown(39))
  286.    {
  287.       keyPressed = _root.moveChar(ob,1,0);
  288.    }
  289.    else if(key.isDown(37))
  290.    {
  291.       keyPressed = _root.moveChar(ob,-1,0);
  292.    }
  293.    else if(key.isDown(38))
  294.    {
  295.       keyPressed = _root.moveChar(ob,0,-1);
  296.    }
  297.    else if(key.isDown(40))
  298.    {
  299.       keyPressed = _root.moveChar(ob,0,1);
  300.    }
  301.    if(!KeyPressed)
  302.    {
  303.       ob.clip.mychar.gotoAndStop(1);
  304.    }
  305.    else
  306.    {
  307.       ob.clip.mychar.play();
  308.    }
  309.    enemyBrain();
  310. }
  311. function moveChar(ob, dirx, diry)
  312. {
  313.    getMyCorners(ob.x,ob.y + ob.speed * diry,ob);
  314.    if(diry == -1)
  315.    {
  316.       if(ob.upleft and ob.upright)
  317.       {
  318.          ob.y += ob.speed * diry;
  319.       }
  320.       else
  321.       {
  322.          ob.y = ob.ytile * game.tileH + ob.height;
  323.       }
  324.    }
  325.    if(diry == 1)
  326.    {
  327.       if(ob.downleft and ob.downright)
  328.       {
  329.          ob.y += ob.speed * diry;
  330.       }
  331.       else
  332.       {
  333.          ob.y = (ob.ytile + 1) * game.tileH - ob.height;
  334.       }
  335.    }
  336.    getMyCorners(ob.x + ob.speed * dirx,ob.y,ob);
  337.    if(dirx == -1)
  338.    {
  339.       if(ob.downleft and ob.upleft)
  340.       {
  341.          ob.x += ob.speed * dirx;
  342.       }
  343.       else
  344.       {
  345.          ob.x = ob.xtile * game.tileW + ob.width;
  346.       }
  347.    }
  348.    if(dirx == 1)
  349.    {
  350.       if(ob.upright and ob.downright)
  351.       {
  352.          ob.x += ob.speed * dirx;
  353.       }
  354.       else
  355.       {
  356.          ob.x = (ob.xtile + 1) * game.tileW - ob.width;
  357.       }
  358.    }
  359.    ob.clip._x = ob.x;
  360.    ob.clip._y = ob.y;
  361.    ob.clip.gotoAndStop(dirx + diry * 2 + 3);
  362.    ob.xtile = Math.floor(ob.clip._x / game.tileW);
  363.    ob.ytile = Math.floor(ob.clip._y / game.tileH);
  364.    var itemname = game["item" + ob.ytile + "_" + ob.xtile];
  365.    if(itemname and ob == _root.char)
  366.    {
  367.       game.points = true;
  368.       removeMovieClip(itemname.clip);
  369.       game.items[itemname.position] = 0;
  370.       delete game["item" + ob.ytile + "_" + ob.xtile];
  371.       var name = "enemy3";
  372.       game[name] = new game.Enemyp3();
  373.       game.clip.attachMovie("enemy1",name,10005);
  374.       game[name].clip = game.clip[name];
  375.       game[name].xtile = 4;
  376.       game[name].ytile = 2;
  377.       game[name].width = game.clip[name]._width / 2;
  378.       game[name].height = game.clip[name]._height / 2;
  379.       game[name].x = game[name].xtile * game.tileW + game.tileW / 2;
  380.       game[name].y = game[name].ytile * game.tileH + game.tileH / 2;
  381.       game[name].clip._x = game[name].x;
  382.       game[name].clip._y = game[name].y;
  383.       if(game.points = true)
  384.       {
  385.          opendoorname = "t_" + unlockY + "_" + unlockX;
  386.          game.clip[opendoorname].gotoAndStop(4);
  387.          game.Tile2.prototype.walkable = true;
  388.       }
  389.    }
  390.    if(_root.char.ytile == unlockY && _root.char.xtile == unlockX)
  391.    {
  392.       removeMovieClip(_root.tiles);
  393.       _root.gotoAndStop(11);
  394.    }
  395.    if(_root.char.ytile == 4 && _root.char.xtile == 4)
  396.    {
  397.       var name = "enemy3";
  398.       game[name].xMove = 0;
  399.       game[name].yMove = -1;
  400.       game[name].speed = 0.5;
  401.    }
  402.    return true;
  403. }
  404. function getMyCorners(x, y, ob)
  405. {
  406.    ob.downY = Math.floor((y + ob.height - 1) / game.tileH);
  407.    ob.upY = Math.floor((y - ob.height) / game.tileH);
  408.    ob.leftX = Math.floor((x - ob.width) / game.tileW);
  409.    ob.rightX = Math.floor((x + ob.width - 1) / game.tileW);
  410.    ob.upleft = game["t_" + ob.upY + "_" + ob.leftX].walkable;
  411.    ob.downleft = game["t_" + ob.downY + "_" + ob.leftX].walkable;
  412.    ob.upright = game["t_" + ob.upY + "_" + ob.rightX].walkable;
  413.    ob.downright = game["t_" + ob.downY + "_" + ob.rightX].walkable;
  414. }
  415. fscommand("allowscale",true);
  416. myMap1 = [[5,5,5,5,1,5,5,5,5,5],[5,5,5,1,2,1,5,5,5,5],[5,5,1,0,0,0,1,5,5,5],[5,5,5,1,0,1,1,1,1,5],[5,5,1,0,0,0,0,0,0,1],[5,5,5,1,0,1,1,0,1,5],[5,5,5,1,0,1,5,1,5,5],[5,5,1,0,0,1,5,5,5,5],[5,5,5,1,1,5,5,5,5,5]];
  417. game = {tileW:45,tileH:45,currentMap:1,points:false};
  418. game.Tile0 = function()
  419. {
  420. };
  421. game.Tile0.prototype.walkable = true;
  422. game.Tile0.prototype.frame = 1;
  423. game.Tile1 = function()
  424. {
  425. };
  426. game.Tile1.prototype.walkable = false;
  427. game.Tile1.prototype.frame = 2;
  428. game.Tile2 = function()
  429. {
  430. };
  431. game.Tile2.prototype.walkable = false;
  432. game.Tile2.prototype.frame = 3;
  433. game.Tile5 = function()
  434. {
  435. };
  436. game.Tile5.prototype.walkable = false;
  437. game.Tile5.prototype.frame = 5;
  438. unlockY = 1;
  439. unlockX = 4;
  440. char = {xtile:3,ytile:7,speed:3};
  441. startingXtile = char.xtile;
  442. startingYtile = char.ytile;
  443. mylifes.gotoAndStop(_root.lifes);
  444. myEnemies = [[1,3,4],[2,4,2]];
  445. game.Enemyp1 = function()
  446. {
  447. };
  448. game.Enemyp1.prototype.xMove = 1;
  449. game.Enemyp1.prototype.yMove = 0;
  450. game.Enemyp1.prototype.speed = 1;
  451. game.Enemyp2 = function()
  452. {
  453. };
  454. game.Enemyp2.prototype.xMove = 1;
  455. game.Enemyp2.prototype.yMove = 0;
  456. game.Enemyp2.prototype.speed = 1;
  457. game.Enemyp3 = function()
  458. {
  459. };
  460. game.Enemyp3.prototype.xMove = 0;
  461. game.Enemyp3.prototype.yMove = 1;
  462. game.Enemyp3.prototype.speed = 1;
  463. myItems = [[1,7,5]];
  464. game.Item1 = function()
  465. {
  466. };
  467. game.Item1.prototype.points = false;
  468. _root.buildMap(_root["myMap" + game.currentMap]);
  469. stop();
  470.